package magicboard.classifier
{
	import flash.geom.Point;
	
	import magicboard.classifier.feature.FeatureClassifier;
	import magicboard.model.InkStroke;
	import magicboard.model.Model;
	
	import mx.utils.StringUtil;
	
	public class FileTest
	{
		public function FileTest(){
		}

		// --------------------------------------------------------------------------------------------
		public function get expected():String { return _output;}
		public function get received():String { return _type;}
		
		// --------------------------------------------------------------------------------------------
		/**
		 * @return true if tast succeeded
		 */
		public function runTest(data:String):Boolean{
			var classifier:Classifier = new FeatureClassifier();
			
			
			parseTest(data);
			_type = classifier.classify(_input);
			_type = _type.toLowerCase();
			
			return (_output == _type);
		}
		
		// --------------------------------------------------------------------------------------------
		private function parseTest(data:String):void{
			var lines:Array = data.split(/[\n\r]/);
			var mode:String = "";
      
      _model = new Model();
      _output = "";

			for each ( var line:String in lines ){

				line = StringUtil.trim(line).toLowerCase();
	      // Ignore comments.
      	if(line.length == 0 || line.indexOf("#") == 0){
      		// This is comment or empty line ignore it
      	}else if(line.indexOf("[") == 0){
      		mode = line.substr(1, line.indexOf("]")-1); 
      	}else{
					var map:Array = line.split(":");
					if(map.length == 2){
						if(mode == "input"){
							parseInput(map[0], map[1]);
						}else if(mode == "model"){
							_model.loadFromString(line);
						}else if(mode == "output"){
							parseOutput(map[0], map[1]);
						}
					}      		
      	}
	    }
		}
		
		// --------------------------------------------------------------------------------------------
		private function parseInput(key:String, value:String):void{
			var points:Array = value.match(/[\d\.]+/g);
			
      _input = new InkStroke();
      for(var i:int = 1; i < points.length; i += 2){
      	_input.addPoint(new Point(points[i-1], points[i]));
      }
		}
		
		// --------------------------------------------------------------------------------------------
		private function parseOutput(key:String, value:String):void{
			_output = StringUtil.trim(value);
		}
		
		
		// --------------------------------------------------------------------------------------------
		// Private members
		private var _input:InkStroke;		// Input points
		private var _model:Model;				// Model
		private var _output:String;			// output type
		private var _type:String;
	}
}